package com.didi.gulimall.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.didi.gulimall.api.MemberApiService;
import com.didi.gulimall.auth.service.OAuth2Service;
import com.didi.gulimall.auth.vo.SocialUser;
import com.didi.gulimall.common.constant.AuthServerConstant;
import com.didi.gulimall.common.constant.SystemConstant;
import com.didi.gulimall.common.to.MemberResponseVo;
import com.didi.gulimall.common.utils.HttpUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.config.WeiboConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 10626
 */
@Service
@Slf4j
public class OAuth2ServiceImpl implements OAuth2Service {

    @Autowired
    private MemberApiService memberApiService;

    @Value("${gulimall.home}")
    private String homeHtml;

    @Value("${gulimall.login}")
    private String loginHtml;

    @Autowired
    private WeiboConfigProperties weiboConfigProperties;

    @Override
    public String oauthWeiboSuccess(String code, HttpSession session, RedirectAttributes redirectAttributes) throws Exception {
        Map<String, String> header = new HashMap<>();
        Map<String, String> query = new HashMap<>();
        Map<String, String> map = new HashMap<>();

        map.put("client_id", weiboConfigProperties.getClientId());
        map.put("client_secret", weiboConfigProperties.getClientSecret());
        map.put("grant_type", weiboConfigProperties.getGrantType());
        map.put("redirect_uri", weiboConfigProperties.getRedirectUri());
        map.put("code", code);
        HttpResponse res = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", header, query, map);
        if (res.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
            String json = EntityUtils.toString(res.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            R r = memberApiService.oauthweibologin(socialUser);
            if (r.getCode() != 0) {
                redirectAttributes.addFlashAttribute(SystemConstant.ERRORS, r.getData(SystemConstant.MSG, new TypeReference<String>() {
                }));
                log.error("错误信息" + r.getMsg());
                return "redirect:" + loginHtml;
            }
            MemberResponseVo user = r.getData("user", new TypeReference<MemberResponseVo>() {
            });
            session.setAttribute(AuthServerConstant.LOGIN_USER, user);
            return "redirect:" + homeHtml;
        }
        redirectAttributes.addFlashAttribute(SystemConstant.ERRORS, AuthServerConstant.PARAMETER_IS_INCORRECT);
        return "redirect:" + loginHtml;
    }
}
